package com.configurations;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class TaskExecutorConfig {

  @Value("${spring.pool.corePoolSize}")
  private Integer corePoolSize;

  @Value("${spring.pool.maxPoolSize}")
  private Integer maxPoolSize;

  @Value("${spring.pool.queueCapacity}")
  private Integer queueCapacity;

  @Bean
  public ThreadPoolTaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    if (corePoolSize != null) {
      pool.setCorePoolSize(corePoolSize);
    }
    if (maxPoolSize != null) {
      pool.setMaxPoolSize(maxPoolSize);
    }
    if (queueCapacity != null) {
      pool.setQueueCapacity(queueCapacity);
    }
    return pool;
  }

}
